package chap6

object Chap6 {
  //1. Write an object Conversionswith methods inchesToCentimeters,  gallonsToLiters, and
  //   milesToKilometers.
  object Conversions {
    def inchesToCentimeters(inches: Double): Double = inches * 2.54

    def gallonsToLiters(gal: Double) = gal * 4.5

    def milesToKilometers(miles: Float): Double = miles * 1.6

  }
  // 2. The preceding problem wasn't very object-oriented. Provide a general
  //    super-class UnitConversion and define objects InchesToCentimeters,
  //    GallonsToLiters, and MilesToKilometers that extend it.
  class UnitConversion(val factor: Double) {
    private def convert(value: Double): Double = value * factor
    def apply(value: Double) = convert(value)
  }

  object InchesToCentimeters extends UnitConversion(2.54)
  object GallonsToLiters extends UnitConversion(4.5)
  object MilesToKilometers extends UnitConversion(1.6)

  //3. Define an Origin object that extends java.awt.Point. Why is this not actually a
  //   good idea? (Have a close look at the methods of the Point class.)  
  object Origin extends java.awt.Point {
  }

  //4. Define a Point class with a companion object so that you can construct Point
  //instances as Point(3, 4), without using new.
  object Point {
    def apply(x: Int, y: Int) = new Point(x, y)
  }
  class Point(val x: Int, val y: Int) {
    override def toString = "(" + x + ", " + y + ")"
  }
  //6. Write an enumeration describing the four playing card suits so that the toString
  //method returns ♣, ♦, ♥, or ♠.
  object Card extends Enumeration {
    type Card = Value
    val Club = Value("♣")
    val Diamond = Value("♦")
    val Heart = Value("♥")
    val Spade = Value("♠")
    override def toString = "(" + Club + ", " + Diamond + ", " + Heart + ", " + Spade + ")"

  }

  import Card._
  //7. Implement a function that checks whether a card suit value from the preceding exercise is red.
  def isRed(card: Card) = card == Diamond || card == Heart

  //8. Write an enumeration describing the eight corners of the RGB color cube. As
  //IDs, use the color values (for example, 0xff0000 for Red).
  object RGBCube extends Enumeration {
    val Black = Value(0x000000, "Black")
    val Red = Value(0xff0000, "Red")
    val Green = Value(0x00ff00, "Green")
    val Yellow = Value(0xffff00, "Yellow")
    val Blue = Value(0x0000ff, "Blue")
    val Cyan = Value(0x00ffff, "Cyan")
    val Magenta = Value(0xff00ff, "Magenta")
    val White = Value(0xffffff, "White")
  }
  
  def main(args: Array[String]): Unit = {
    //println(Conversions.inchesToCentimeters(3))
    //println(InchesToCentimeters.apply(3))
    //println(InchesToCentimeters(3))
    //    println(Point(3, 4).toString())
    println(Card.toString())
    println(isRed(Diamond))
  }
}